import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CountDownLatchDemo {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService executorService= Executors.newFixedThreadPool(4);
        //构造方法的参数就是拆分出来的任务个数
        CountDownLatch countDownLatch=new CountDownLatch(20);
        for(int i=0;i<20;i++) {
            int id = i;
            executorService.submit(() -> {
                System.out.println("下载任务" + id + "开始执行");
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("下载任务" + id + "结束执行");
                countDownLatch.countDown();//完成
            });
        }
            //当countDownLatch收到了20个"完成",那么就是所有任务都完成了
            countDownLatch.await();//await=all wait
            System.out.println("所有任务都完成了");
    }
}
